﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
using static AccountOperations.AccountOperations;

namespace Bank_database_system.业务部.账户操作
{
    public partial class 修改用户信息a : UserControl
    {
        private string name = "";
        private string ID = "";
        private string address = "";
        private string phone = "";
        private int pageSize = 11;
        修改用户信息b modifyB = new 修改用户信息b();
        public event EventHandler ButtonBack;
        public 修改用户信息a()
        {
            InitializeComponent();
            button1_Click(this, EventArgs.Empty);
            modifyB.ButtonBack += BackToA;
        }

        private void BackToA(object sender, EventArgs e)
        {
            // 修改柜台业务B中的控件状态      
            panel1.Visible = false;
            panel1.Controls.Clear();
            button1_Click(this, e);
        }

        private void CustomerSet()
        {
            int page = Convert.ToInt32(label7.Text);
            //其它
            DataTable dataTable = CustomerInquiry(ID, name, address, phone, page, pageSize);
            var filteredRows = dataTable.AsEnumerable()
                                        .Where(row => !row.Field<string>("NAME").Equals("系统", StringComparison.OrdinalIgnoreCase))
                                        .ToList();
            if (filteredRows.Any())
            {
                dataTable = filteredRows.CopyToDataTable();
            }
            else
            {
                dataTable = dataTable.Clone(); // 或者创建一个空的 DataTable
            }

            try
            {
                dataGridView1.RowHeadersVisible = false;
                dataGridView1.AutoGenerateColumns = false; // 禁用自动生成列
                dataGridView1.AllowUserToAddRows = false; // 禁用添加空白行
                dataGridView1.Columns.Clear(); // 清除已有列
                dataGridView1.DoubleBuffered(true); //双缓冲

                // 添加 IDENTIFICATION_NUMBER 列
                DataGridViewTextBoxColumn IDColumn = new DataGridViewTextBoxColumn
                {
                    Name = "IDENTIFICATION_NUMBER",
                    HeaderText = "身份证号",
                    DataPropertyName = "IDENTIFICATION_NUMBER",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(IDColumn);

                // 添加 NAME 列
                DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn
                {
                    Name = "NAME",
                    HeaderText = "姓名",
                    DataPropertyName = "NAME",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(nameColumn);

                // 添加 ADDRESS 列
                DataGridViewTextBoxColumn addressColumn = new DataGridViewTextBoxColumn
                {
                    Name = "ADDRESS",
                    HeaderText = "住址",
                    DataPropertyName = "ADDRESS",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(addressColumn);

                // 添加 PHONENUMBER 列
                DataGridViewTextBoxColumn phoneColumn = new DataGridViewTextBoxColumn
                {
                    Name = "PHONENUMBER",
                    HeaderText = "电话号码",
                    DataPropertyName = "PHONENUMBER",
                    ReadOnly = true,
                    DefaultCellStyle = new DataGridViewCellStyle
                    {
                        Alignment = DataGridViewContentAlignment.MiddleCenter
                    },
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(phoneColumn);

                DataGridViewButtonColumn modifyButtonColumn = new DataGridViewButtonColumn
                {
                    Name = "Modify",
                    HeaderText = "查看信息",
                    Text = "查看",
                    UseColumnTextForButtonValue = true,
                    Width = 100,
                    HeaderCell = { Style = { Alignment = DataGridViewContentAlignment.MiddleCenter } } // 设置列标题居中
                };
                dataGridView1.Columns.Add(modifyButtonColumn);
                dataGridView1.CellClick += DataGridView1_CellClick;

                dataGridView1.DataSource = dataTable; // 绑定数据
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 列宽自动调整模式
            }
            catch (Exception ex)
            {
                // 错误处理

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            button1_Click(sender, e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //筛选
            ID = textBox1.Text;
            name = textBox4.Text;
            address = textBox2.Text;
            phone = textBox3.Text;
            int total = GetCustomerNum(ID, name, address, phone);
            if (total == 0)
            {
                label7.Text = "1";
                label8.Text = "1";
                dataGridView1.DataSource = null;
                return;
            }
            if (total % pageSize == 0)
            {
                label8.Text = (total / pageSize).ToString();
            }
            else
            {
                label8.Text = (total / pageSize + 1).ToString();
            }
            label7.Text = "1";
            CustomerSet();
        }

        private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // 确保点击的是数据行和按钮列
            if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Modify"].Index)
            {
                // 获取当前行的数据
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                string ID = row.Cells["IDENTIFICATION_NUMBER"].Value?.ToString();

                panel1.Visible = true;
                panel1.Controls.Clear();
                modifyB.Information_Set(ID);
                panel1.Controls.Add(modifyB);
                panel1.BringToFront();//显示置顶
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            ButtonBack?.Invoke(this, EventArgs.Empty);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            int page = Convert.ToInt32(label7.Text);
            if (page > 1)
            {
                page--;
                label7.Text = Convert.ToString(page);
                CustomerSet();
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            int page = Convert.ToInt32(label7.Text);
            if (page < Convert.ToInt32(label8.Text))
            {
                page++;
                label7.Text = Convert.ToString(page);
                CustomerSet();
            }
        }
    }
}
